package com.honey.compilation.sql.ddl;

import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.compilation.sql.SqlTable;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;

/**
 * 删除 table, SQL 92 定义如下:<br />
 *  DROP TABLE <table name> <drop behaviour>
 *  
 * @author Administrator
 *
 */

public class DropTable  extends  Drop implements SqlScript {
	
	/** 数据库表 */
	private SqlTable table;
	
	/** 是否添加 IF EXISTS 判断(SQL 92中没有定义这个语法) */
	private boolean exists = false;
	
	/**
	 * 构造函数
	 * @param tableName 表的名称
	 */
	public DropTable(String tableName){
		this.table = new SqlTable(tableName);
	}
	

	/**
	 * 构造函数
	 * @param table 数据库表对象
	 */
	public DropTable(SqlTable table){
		this.table = table;
	}
	
	
	/**
	 * 构造函数
	 * @param schema 数据库结构名称
	 * @param tableName 表的名称
	 */
	public DropTable(String schema,String tableName){
		this.table = new SqlTable(schema,tableName);
	}
	
	/**
	 * 获取数据库结构名称
	 * @return
	 */
	public String getSchema() {
		return table.getSchema();
	}

	/**
	 * 设定数据库结构名称
	 * @param schema 数据结构名称
	 */
	public void setSchema(String schema) {
		this.table.setSchema(schema);
	}
	
	/**
	 * 是否添加判断语句( if exists)
	 * @return true:是  false:否
	 */
	public boolean isExists() {
		return exists;
	}


	/**
	 * 是否添加判断语句( if exists)
	 * @param exists  true:是  false:否
	 */
	public void setExists(boolean exists) {
		this.exists = exists;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.sql.ddl.Drop#compiledContent(int)
	 */
	@Override
	public CharacterBuilder compiledContent(int indentLevel) throws CompilationException{
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		
		answer.append( super.compiledContent( indentLevel ) );
		answer.append(SqlKeyWord.TABLE);
		if ( exists )
			answer.append(SqlKeyWord.IF).append( SqlKeyWord.EXISTS ) ;

		answer.append( table.compiledContent(0) );
		answer.append( SqlKeyWord.OPERATION_SEMICOLON );
		return answer;
	}
}
